package com.yicong.ants.ui.video.live

import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.ToastUtils
import com.cchao.simplelib.ui.fragment.BaseStatefulFragment
import com.x.livesdk.Room
import com.x.livesdk.util.http.DataCallBack
import com.x.livesdk.util.http.HttpUtil
import com.yicong.ants.R
import com.yicong.ants.databinding.LiveFragmentAppBinding
import com.yicong.ants.ui.video.adapter.LiveViewpagerAdapter

class LiveFragment : BaseStatefulFragment<LiveFragmentAppBinding>() {
    lateinit var adapter: LiveViewpagerAdapter
    var onLiveAllCompletionListener: (() -> Unit)? = null

    override fun getLayoutId() = R.layout.live_fragment_app

    override fun initEventAndData() {
        adapter = LiveViewpagerAdapter(this)
        adapter.onLiveAllCompletionListener = {
            onLiveAllCompletionListener?.invoke()
        }
        mDataBind.viewpager.adapter = adapter
        mDataBind.viewpager.offscreenPageLimit = 3
        mDataBind.viewpager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                adapter.setPageSelected(position)
            }
        })
        mDataBind.swipeRefresh.setOnRefreshListener {
            onLoadData()
        }
        mDataBind.swipeRefresh.setOnLoadMoreListener {
            HttpUtil.api.loadRooms().enqueue(
                DataCallBack(
                    onSuccess = {
                        mDataBind.swipeRefresh.finishLoadMore()
                        val newItems = arrayListOf<Room>()
                        it.forEach { remoteItem ->
                            if (!adapter.liveItems.contains(remoteItem)) {
                                newItems.add(remoteItem)
                            }
                        }
                        if (newItems.isEmpty()) {
                            ToastUtils.showLong("暂时没有更多直播")
                            return@DataCallBack
                        }
                        adapter.addData(newItems)
                        mDataBind.viewpager.postDelayed({
                            mDataBind.viewpager.setCurrentItem(adapter.liveItems.size - 1, true)
                        }, 500)
                    },
                    onFailure = {
                        mDataBind.swipeRefresh.finishLoadMore()
                        ToastUtils.showLong(it.message)
                    }
                )
            )
        }
        onLoadData()
    }

    override fun onLoadData() {
        HttpUtil.api.loadRooms().enqueue(
            DataCallBack(
                onSuccess = {
                    mDataBind.swipeRefresh.finishRefresh()
                    if (it.isEmpty()) {
                        onLiveAllCompletionListener?.invoke()
                        return@DataCallBack
                    }
                    adapter.setNewData(it)
                    if (!mIsVisible) {
                        mDataBind.viewpager.postDelayed({
//                            adapter.stopAll()
                        }, 2000)
                    }
                },
                onFailure = {
                    mDataBind.swipeRefresh.finishRefresh()
                    ToastUtils.showLong(it.message)
                })
        )
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!isAdded) {
            return
        }
        childFragmentManager.fragments.forEach { fragment ->
            fragment.onHiddenChanged(hidden || !mIsVisible)
        }
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (!isAdded) {
            return
        }
        childFragmentManager.fragments.forEach { fragment ->
            fragment.onHiddenChanged(!isVisibleToUser || mIsHidden)
        }
    }
}